package com.project.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.project.domain.bo.MiddleCheckOrCompleteBo;
import com.project.domain.entity.BizProjectMiddleCheck;
import com.project.domain.vo.BizProjectMiddleCheckVo;
import com.project.domain.vo.NavigationForProVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface BizProjectMiddleCheckMapper extends BaseMapper<BizProjectMiddleCheck> {

    /**
     * 列表
     * @param page
     * @param queryWrapper
     * @param bizProjectMiddleCheck
     * @return
     */
    @Select({
            "<script>",
            "select pb.Name as proName,pb.CHARGER_NAME as chargerName,pb.CHARGER_CODE as chargerCode,pb.UNIT_ID as unitId,suf.UPLOADFILENAME as filename,zb.*,Date(zb.MIDDLE_CHECK_DATE) as middleCheckDate from biz_project_middle_check zb ",
            "left join v_project pb on pb.ID = zb.PROJECT_ID ",
            "left join biz_unit bu on pb.unit_id = bu.id ",
            "left join dm_project_type dpt on zb.PROJECT_TYPE = dpt.CODE ",
            "left join sys_upload_file suf on zb.FILE_ID = suf.ID ",
            "<if test='ew.customSqlSegment != null and ew.customSqlSegment != \"\"'>",
            "${ew.customSqlSegment} ",
            "</if>",
            "<if test='ew.customSqlSegment == null or ew.customSqlSegment == \"\"'>",
            "where 1 = 1 ",
            "</if>",
            "<if test='bizProjectMiddleCheck.proName != null and bizProjectMiddleCheck.proName.trim() != \"\"'>",
            "and pb.name like CONCAT('%', '${bizProjectMiddleCheck.proName}', '%') ",
            "</if>",
            "<if test='bizProjectMiddleCheck.chargerName != null and bizProjectMiddleCheck.chargerName != \"\"'>",
            "and pb.CHARGER_NAME like CONCAT('%', '${bizProjectMiddleCheck.chargerName}', '%') ",
            "</if>",
            "<if test='bizProjectMiddleCheck.unitId != null and bizProjectMiddleCheck.unitId != \"\"'>",
            "and pb.UNIT_ID in ('${bizProjectMiddleCheck.unitId}') ",
            "</if>",
            "<if test='bizProjectMiddleCheck.checkStatus != null and bizProjectMiddleCheck.checkStatus != \"\"'>",
            "and zb.CHECKSTATUS in ('${bizProjectMiddleCheck.checkStatus}') ",
            "</if>",
            "<if test='bizProjectMiddleCheck.projectTypeCode != null and bizProjectMiddleCheck.projectTypeCode != \"\"'>",
            "and zb.PROJECT_TYPE in ('${bizProjectMiddleCheck.projectTypeCode}') ",
            "</if>",
            "order by zb.${bizProjectMiddleCheck.orderStr} ${bizProjectMiddleCheck.orderType}",
            "</script>"
    })
    IPage<BizProjectMiddleCheckVo> selectListInfos(IPage<BizProjectMiddleCheckVo> page, @Param(Constants.WRAPPER) Wrapper<BizProjectMiddleCheckVo> queryWrapper, @Param("bizProjectMiddleCheck") MiddleCheckOrCompleteBo bizProjectMiddleCheck);

    /**
     * 导出
     * @param queryWrapper
     * @param bizProjectMiddleCheck
     * @return
     */
    @Select({
            "<script>",
            "select pb.CODE as proCode,pb.NAME as proName,pb.CHARGER_NAME,bu.NAME as unitName,dpt.BELONG_TYPE as proTypeName,zb.NOTE as zjNote,",
            "Date(MIDDLE_CHECK_DATE) as middleCheckDate,dc.dict_label as checkstatusName,suf.UPLOADFILENAME as fileName,zb.FILE_ID as fileId,zb.checkstatus ",
            " from biz_project_middle_check zb ",
            "left join v_project pb on pb.ID = zb.PROJECT_ID ",
            "left join biz_unit bu on pb.unit_id = bu.id ",
            "left join dm_project_type dpt on zb.PROJECT_TYPE = dpt.CODE ",
            "left join sys_upload_file suf on zb.FILE_ID = suf.ID ",
            "left join (select dict_label,dict_value from sys_dict_data WHERE dict_type = 'bpm_process_instance_status') dc on dc.dict_value = zb.CHECKSTATUS ",
            "<if test='ew.customSqlSegment != null and ew.customSqlSegment != \"\"'>",
            "${ew.customSqlSegment} ",
            "</if>",
            "<if test='ew.customSqlSegment == null or ew.customSqlSegment == \"\"'>",
            "where 1 = 1 ",
            "</if>",
            "<if test='bizProjectMiddleCheck.proName != null and bizProjectMiddleCheck.proName.trim() != \"\"'>",
            "and pb.name like CONCAT('%', '${bizProjectMiddleCheck.proName}', '%')  ",
            "</if>",
            "<if test='bizProjectMiddleCheck.chargerName != null and bizProjectMiddleCheck.chargerName != \"\"'>",
            "and pb.CHARGER_NAME like CONCAT('%', '${bizProjectMiddleCheck.chargerName}', '%') ",
            "</if>",
            "<if test='bizProjectMiddleCheck.unitId != null and bizProjectMiddleCheck.unitId != \"\"'>",
            "and pb.UNIT_ID in ('${bizProjectMiddleCheck.unitId}') ",
            "</if>",
            "<if test='bizProjectMiddleCheck.checkStatus != null and bizProjectMiddleCheck.checkStatus != \"\"'>",
            "and zb.CHECKSTATUS in ('${bizProjectMiddleCheck.checkStatus}') ",
            "</if>",
            "<if test='bizProjectMiddleCheck.projectTypeCode != null and bizProjectMiddleCheck.projectTypeCode != \"\"'>",
            "and zb.PROJECT_TYPE in ('${bizProjectMiddleCheck.projectTypeCode}') ",
            "</if>",
            "<if test='bizProjectMiddleCheck.proId != null and bizProjectMiddleCheck.proId != \"\"'>",
            "and zb.PROJECT_ID in ('${bizProjectMiddleCheck.proId}') ",
            "</if>",
            "order by zb.${bizProjectMiddleCheck.orderStr} ${bizProjectMiddleCheck.orderType}",
            "</script>"
    })
    IPage<BizProjectMiddleCheckVo> selectListInfosForExport(IPage<BizProjectMiddleCheckVo> page,@Param(Constants.WRAPPER) Wrapper<BizProjectMiddleCheckVo> queryWrapper, @Param("bizProjectMiddleCheck") MiddleCheckOrCompleteBo bizProjectMiddleCheck);


    /**
     * 统计
     * @param queryWrapper
     * @param bizProjectMiddleCheck
     * @return
     */
    @Select({
            "<script>",
            "SELECT sdd.dict_label AS className, sdd.dict_value as classValue, COALESCE(COUNT(zbAll.CHECKSTATUS), 0) AS classCount ",
            "FROM ",
            "(SELECT DISTINCT dict_label,dict_value FROM sys_dict_data where dict_type ='bpm_process_instance_status' ) AS sdd  ",
            "LEFT join ",
            "(select zb.*, pb.NAME,pb.CHARGER_NAME,pb.UNIT_ID from biz_project_middle_check zb left join v_project pb on pb.ID = zb.PROJECT_ID ",
            "<if test='ew.customSqlSegment != null and ew.customSqlSegment != \"\"'>",
            "${ew.customSqlSegment} ",
            "</if>",
            "<if test='ew.customSqlSegment == null or ew.customSqlSegment == \"\"'>",
            "where 1 = 1 ",
            "</if>",
            "<if test='bizProjectMiddleCheck.proName != null and bizProjectMiddleCheck.proName.trim() != \"\"'>",
            "and pb.NAME like CONCAT('%', '${bizProjectMiddleCheck.proName}', '%') ",
            "</if>",
            "<if test='bizProjectMiddleCheck.chargerName != null and bizProjectMiddleCheck.chargerName != \"\"'>",
            "and pb.CHARGER_NAME like CONCAT('%', '${bizProjectMiddleCheck.chargerName}', '%') ",
            "</if>",
            "<if test='bizProjectMiddleCheck.unitId != null and bizProjectMiddleCheck.unitId != \"\"'>",
            "and pb.UNIT_ID in ('${bizProjectMiddleCheck.unitId}') ",
            "</if>",
            "<if test='bizProjectMiddleCheck.checkStatus != null and bizProjectMiddleCheck.checkStatus != \"\"'>",
            "and zb.CHECKSTATUS in ('${bizProjectMiddleCheck.checkStatus}') ",
            "</if>",
            "<if test='bizProjectMiddleCheck.projectTypeCode != null and bizProjectMiddleCheck.projectTypeCode != \"\"'>",
            "and zb.PROJECT_TYPE in ('${bizProjectMiddleCheck.projectTypeCode}') ",
            "</if>",
            " ) zbAll ON zbAll.CHECKSTATUS = sdd.dict_value  ",
            "GROUP BY sdd.dict_label, sdd.dict_value ORDER BY sdd.dict_value",
            "</script>"
    })
    List<NavigationForProVo.NameAndCount> selectGroups(@Param(Constants.WRAPPER) Wrapper<NavigationForProVo.NameAndCount> queryWrapper, @Param("bizProjectMiddleCheck") MiddleCheckOrCompleteBo bizProjectMiddleCheck);

    @Select({
            "<script>",
            "select zb.*,Date(MIDDLE_CHECK_DATE) as middleCheckDate, suf.UPLOADFILENAME as filename from biz_project_middle_check zb ",
            "left join sys_upload_file suf on zb.FILE_ID = suf.ID ",
            "where zb.ID = '${ID}'",
            "</script>"
    })
    BizProjectMiddleCheck selectDetail(@Param("ID") String ID);
}
